//package cn.maidouya.hrm.filter;
//
//import com.alibaba.fastjson.JSON;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.SimpleGrantedAuthority;
//import org.springframework.security.core.context.SecurityContext;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
//import org.springframework.stereotype.Component;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.Collection;
//import java.util.List;
//import java.util.Map;
//
//@Component
//public class BaseTokenAuthenticatonFilter extends OncePerRequestFilter {
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        String tokenJson = request.getHeader("token");
//        Map map = JSON.parseObject(tokenJson, Map.class);
//        String username = map.get("principal").toString();
//        List<String> authoritiesStr = (List<String>)map.get("authorities");
//        List<SimpleGrantedAuthority> authorities = new ArrayList<>(authoritiesStr.size());
//
//        authorities.stream().forEach(uthor->{
//            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(uthor.getAuthority());
//            authorities.add(simpleGrantedAuthority);
//        });
//
//        UsernamePasswordAuthenticationToken token =
//                new UsernamePasswordAuthenticationToken(username, null, authorities);
//        //WebAuhenticationDetailsSource
//        token.setDetails(new WebAuthenticationDetailsSource());
//
//        SecurityContextHolder.getContext().setAuthentication(token);
//
//
//
//        filterChain.doFilter(request, response);
//
//    }
//}
